package com.platform.controller;

import java.util.List;
import java.util.Map;

import com.platform.entity.ApiWithdrawBillVo;
import com.platform.entity.UserEntity;
import com.platform.service.UserService;
import com.platform.service.WithdrawBillService;
import com.platform.utils.push.UPush;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.platform.entity.WithdrawBillEntity;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 提现管理Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-06-03 15:04:03
 */
@Controller
@RequestMapping("withdrawbill")
public class WithdrawBillController {
    @Autowired
    private WithdrawBillService apiWithdrawBillService;
    @Autowired
    private UserService userService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("apiwithdrawbill:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<WithdrawBillEntity> apiWithdrawBillList = apiWithdrawBillService.queryList(query);
        for (WithdrawBillEntity info : apiWithdrawBillList) {

            UserEntity userEntity = userService.queryObject(info.getUserId());
            if (userEntity != null) {
                info.setUserMobile(userEntity.getMobile());
                info.setNickName(userEntity.getNickname());
            } else {
                info.setUserMobile("-");
                info.setNickName("-");
            }

        }


        int total = apiWithdrawBillService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(apiWithdrawBillList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("apiwithdrawbill:info")
    @ResponseBody
    public R info(@PathVariable("id") Long id) {
        WithdrawBillEntity apiWithdrawBill = apiWithdrawBillService.queryObject(id);

        return R.ok().put("apiWithdrawBill", apiWithdrawBill);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("apiwithdrawbill:save")
    @ResponseBody
    public R save(@RequestBody WithdrawBillEntity apiWithdrawBill) {
        apiWithdrawBillService.save(apiWithdrawBill);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("apiwithdrawbill:update")
    @ResponseBody
    public R update(@RequestBody WithdrawBillEntity withdrawBillEntity) {
        WithdrawBillEntity info = apiWithdrawBillService.queryObject(withdrawBillEntity.getId());
        UserEntity user = userService.queryObject(info.getUserId());

        //0 提现审核中 1 提现失败 2 提现成功
        if (info.getStatus() == 0) {
            apiWithdrawBillService.update(withdrawBillEntity);
            if (withdrawBillEntity.getStatus() == 1) {
                //返还佣金
                userService.reback("add", info.getUserId(), info.getAmount(), info.getUserId());
                UPush.push(user.getMobileType(), user.getDeviceToken(), "提现失败", withdrawBillEntity.getRemark());
                return R.ok();
            }
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("apiwithdrawbill:delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids) {
        apiWithdrawBillService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<WithdrawBillEntity> list = apiWithdrawBillService.queryList(params);

        return R.ok().put("list", list);
    }
}
